<template>
  <div>
    <!-- 面包屑部分 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片部分 -->
    <el-card>
      <el-input
        @clear="getgoodsList()"
        @blur="losefocus($event)"
        class="searchinput"
        placeholder="请输入内容"
        v-model="goodsInfo.query"
        clearable
      >
        <el-button slot="append" @click="getgoodsList()" icon="el-icon-search">
        </el-button>
      </el-input>
      <el-button class="addbtn" @click="addgoods" type="primary"
        >添加商品</el-button
      >
      <el-divider></el-divider>
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column
          prop="goods_price"
          width="70px"
          align="center"
          label="商品价格"
        >
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          width="70px"
          align="center"
          label="商品重量"
        >
        </el-table-column>
        <el-table-column width="170px" align="center" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <el-button
              @click="editGoods(scope.row.goods_id)"
              type="primary"
              size="mini"
              icon="el-icon-edit"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              @click="deleteGoods(scope.row.goods_id)"
              size="mini"
              icon="el-icon-delete"
            >
              删除
            </el-button></template
          >
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="goodsInfo.pagenum"
          :page-sizes="[10, 50, 100, 200]"
          :page-size="goodsInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 编辑弹出框 -->
    <el-dialog title="编辑商品信息" :visible.sync="editVisible" width="50%">
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editForm.goods_number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'List',
  created() {
    this.getgoodsList()
  },
  data() {
    return {
      //请求参数
      goodsInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      //商品列表
      goodsList: [],
      //商品总条数
      total: 0,
      // 编辑框是否弹出
      editVisible: false,
      // 编辑数据
      editForm: {
        goods_name: '',
        goods_price: 0,
        goods_weight: 0,
        goods_number: 0,
      },
      editFormRules: {
        goods_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        goods_price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        goods_weight: [{ required: true, message: '请输入商品重量', trigger: 'blur' }],
        goods_number: [{ required: true, message: '请输入商品数量', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 获取商品列表
    getgoodsList() {
      this.$http.get(`http://127.0.0.1:8888/api/private/v1/goods`,
        { params: this.goodsInfo })
        .then((response) => {
          if (response.data.meta.status !== 200) {
            this.$message.error(response.data.meta.msg);
          } else {
            console.log(response);
            this.goodsList = response.data.data.goods;

            // 为总数据条数赋值
            this.total = response.data.data.total
          }
        }).catch((error) => console.log(error))
    },
    // 控制每一页的条数
    handleSizeChange(value) {
      this.goodsInfo.pagesize = value
      this.getgoodsList()
    },
    // 控制当前页数
    handleCurrentChange(value) {
      this.goodsInfo.pagenum = value
      this.getgoodsList()
    },
    // 失去焦点后input框为空则重新获取数据
    losefocus(e) {
      if (!e.target.value) {
        this.getgoodsList()
      }
    },
    // 删除商品
    deleteGoods(id) {
      console.log(id);
      this.$confirm('此操作将永久删除该商品信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete(`http://127.0.0.1:8888/api/private/v1/goods/${id}`)
          .then((response) => {
            if (response.data.meta.status !== 200) {
              this.$message.error(response.data.meta.msg);
            } else {
              this.$message.success(response.data.meta.msg);
              this.getgoodsList()
            }
          }).catch((error) => console.log(error))

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    // 添加商品
    addgoods() {
      this.$router.push('goods/add')

    },
    // 编辑商品
    editGoods(id) {
      // this.editForm.goods_id = id
      this.$http.get(`http://127.0.0.1:8888/api/private/v1/goods/${id}`,)
        .then((response) => {
          if (response.data.meta.status !== 200) {
            this.$message.error(response.data.meta.msg);
          } else {
            console.log(response);
            this.editForm = response.data.data;
          }
        }).catch((error) => console.log(error))
      this.editVisible = true
    },
    // 确定编辑
    edit() {
      this.$refs.editFormRef.validate(valid => {
        if (valid) {
          this.$http.put(`http://127.0.0.1:8888/api/private/v1/goods/${this.editForm.goods_id}`,
            this.editForm)
            .then((response) => {
              if (response.data.meta.status !== 200) {
                console.log('1');
                this.$message.error(response.data.meta.msg);
              } else {
                console.log(response);
                this.$message.success('修改成功');
                this.getgoodsList()
              }
            }).catch((error) => console.log(error))
        } else {
          this.$message.error('请填入完整的表单内容!!')
        }
      })
      this.editVisible = false
    }


  }

}
</script>

<style scoped>
.searchinput {
  width: 40%;
}
.addbtn {
  margin-left: 20px;
}
</style>